package com.cloud.intercept;


import com.alibaba.fastjson.JSON;
import com.cloud.domain.sys.SecretKey;
import com.cloud.service.sys.SecretKeyService;
import com.common.utils.tools.result.BaseReturn;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Component
public class LoginIntercept implements HandlerInterceptor {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    SecretKeyService secretKeyService;


   // private CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String authToken = request.getHeader("authToken");
        String accessToken = request.getHeader("accessToken");
/*        String deviceType = request.getParameter("deviceType");
        String User_Agent = request.getHeader("User-Agent");
        if (User_Agent.contains("Android")||User_Agent.contains("Linux")) {
            System.out.println("Android移动客户端");
            if (User_Agent.contains("MicroMessenger")) {
                System.out.println("微信");
            }
        } else if (User_Agent.contains("iPhone")) {
            System.out .println("iPhone移动客户端");
            if (User_Agent.contains("MicroMessenger")) {
                System.out.println("微信");
            }
        } else if (User_Agent.contains("iPad")) {
            System.out.println("iPad客户端");
            if (User_Agent.contains("MicroMessenger")) {
                System.out.println("微信");
            }
        } else if(User_Agent.contains("Windows")){
            System.out.println("Windows");
        }*/
        boolean resAccessToken = this.verificationAccessToken(accessToken);
        if (!resAccessToken) {
            Object returnMsg = JSON.toJSON(BaseReturn.build("403", "非法请求"));
            response.getWriter().print(returnMsg);
            return false;
        }
        boolean resAuthToken = this.verificationAuthToken(authToken);
        if (!resAuthToken) {
            Object returnMsg = JSON.toJSON(BaseReturn.build("403", "登录失效,请重新登录"));
            response.getWriter().print(returnMsg);
            return false;
        }
        return true;
    }

    private boolean verificationAuthToken(String authToken) {
        if (StringUtils.isBlank(authToken)) {
            //请登录
            return false;
        }
        String token = stringRedisTemplate.opsForValue().get(authToken);
        if (StringUtils.isBlank(token)) {
            return false;
        }
        //重置缓存时间
        stringRedisTemplate.opsForValue().set(authToken, token, 60 * 30, TimeUnit.SECONDS);
        return true;
    }

    //请求不合法
    private boolean verificationAccessToken(String accessToken) {
        if (StringUtils.isBlank(accessToken)) {
            return false;
        }
        List<SecretKey> secretKeys = secretKeyService.getBySecretKey(accessToken);
        int size = secretKeys.size();
        if (size <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
